package com.wyl.server.publish

import android.content.Intent
import android.util.Log
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.SnapHelper
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.wuhenzhizao.titlebar.widget.CommonTitleBar
import com.wyl.base.COMPRESS_SIZE
import com.wyl.libbase.base.NormalActivity
import com.wyl.libbase.extend.autoWired
import com.wyl.pictureselector.GridImageAdapter
import com.wyl.server.R
import kotlinx.android.synthetic.main.publish_activity.*

class PublishActivity : NormalActivity() {

    private val id by lazy { autoWired("id", "") }
    private val type by lazy { autoWired("type", "") }

    private var selectList = mutableListOf<LocalMedia>()

    override fun getLayoutId(): Int = R.layout.publish_activity

    override fun initView() {
        titleBar.apply {
            setListener { _, action, _ ->
                when (action) {
                    CommonTitleBar.ACTION_LEFT_BUTTON -> onBackPressed()
                    else -> {
                    }
                }
            }
        }

        type_text.text = type

        recycler.apply {
            layoutManager = GridLayoutManager(context, 4)
            adapter = mAdapter
        }
    }

    override fun loadData() {

    }

    private val mAdapter by lazy {
        GridImageAdapter(this) {
            // clickAdd
            PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .selectionMedia(selectList)
                .selectionMode(PictureConfig.MULTIPLE)// 多选 or 单选 PictureConfig.MULTIPLE or PictureConfig.SINGLE
                //.maxSelectNum(9)// 最大图片选择数量
//                    .enableCrop(true)// 是否裁剪 true or false
//                    .withAspectRatio(1, 1)// int 裁剪比例 如16:9 3:2 3:4 1:1 可自定义
//                    .freeStyleCropEnabled(false)// 裁剪框是否可拖拽 true or false
//                    .rotateEnabled(false) // 裁剪是否可旋转图片 true or false
//                    .scaleEnabled(true)// 裁剪是否可放大缩小图片 true or false
//                    .circleDimmedLayer(true)// 是否圆形裁剪 true or false
//                    .showCropFrame(false)// 是否显示裁剪矩形边框 圆形裁剪时建议设为false   true or false
//                    .showCropGrid(false)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false    true or false
                .compress(true)// 是否压缩
                .glideOverride(160, 160)// glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
                .minimumCompressSize(COMPRESS_SIZE)// 压缩图片大小
                .forResult(PictureConfig.CHOOSE_REQUEST)
        }.apply {
            setOnItemClickListener { position, v ->
                if (selectList.size > 0) {
                    val media = selectList[position]
                    when (PictureMimeType.pictureToVideo(selectList[position].pictureType)) {
                        1 ->
                            // 预览图片 可自定长按保存路径
                            //PictureSelector.create(MainActivity.this).themeStyle(themeId).externalPicturePreview(position, "/custom_file", selectList);
                            PictureSelector.create(this@PublishActivity)
                                .themeStyle(R.style.picture_default_style)
                                .openExternalPreview(
                                    position,
                                    selectList
                                )
                        2 ->
                            // 预览视频
                            PictureSelector.create(this@PublishActivity)
                                .externalPictureVideo(media.path)
                        3 ->
                            // 预览音频
                            PictureSelector.create(this@PublishActivity)
                                .externalPictureAudio(media.path)
                    }
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            when (requestCode) {
                PictureConfig.CHOOSE_REQUEST -> {
                    // 图片选择结果回调
                    selectList = PictureSelector.obtainMultipleResult(data)
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为tGridImageAdapterrue
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，已取压缩路径为准，因为是先裁剪后压缩的
                    for (media in selectList) {
                        Log.i("图片-----》", media.path)
                    }
                    mAdapter.setList(selectList)
                    mAdapter.notifyDataSetChanged()
                }
            }
        }
    }


}
